install.packages("rJava")
install.packages("RCurl")
require(rJava)
require(RCurl)

setwd("/home/skolnieks9/Dokumenti/Elza /lab03")

hour <- function(x) {
  hrs <- x %/% 100
  return(hrs + (x - 100*hrs)/60)
}

docParams <- "?key=0AgDon7xFOKEudHY2TU9seW4yN1RoQllyelJiY2NhV0E&single=true&gid=0&output=csv"
csvDataLocation = paste("https://docs.google.com/spreadsheet/pub",docParams,sep="")
year <- 2013
cityName <- "Buenosairesa: 69\u00B054'N 27\u00B001'E"
#sunCSV <- getURL(csvDataLocation, ssl.verifypeer=FALSE)
#sunFrame <- read.csv(textConnection(sunCSV))
sunFrame <- read.csv("Buenosairesa.csv")
days <- c(31,28+(year %% 4 == 0),31,30,31,30,31,31,30,31,30,31)

sunrises <- c()
sunsets <- c()
for (i in 1:12) {
  sunrises <- append(sunrises,sapply(sunFrame[,2*i][1:days[i]],hour))
  sunsets<- append(sunsets,(sapply(sunFrame[,2*i+1][1:days[i]],hour)))
}

x <- 1:sum(days)
windows.options(width=15, height=10)
plot(
  c(x,NA,x), c(sunrises[x],NA,sunsets[x]), type="l",
  asp=8, xaxs="i", yaxs="i", ylim=c(0,24),
  col="red", lwd="2",
  axes=FALSE, ann=FALSE, xpd=NA
)
points(sunsets-sunrises,type="l",lwd="2",col="chartreuse3")
monthStarts <- c(1)
for (i in 1:11) {
  monthStarts <- append(monthStarts, tail(monthStarts,1)+days[i])
}
axis(1, at=monthStarts, las=1, lab=paste('01',
    c("01","02","03","04","05","06","07","08","09","10","11","12"),sep="."))
axis(2, las=1, at=3*0:8)
title(main=paste("Dienas ilgums (",cityName,")"), col.main="red")
title(xlab="2012. gads", col.lab=rgb(0,0.5,0))
title(ylab="Viet\u0113jais laiks (UTC+2)", col.lab=rgb(0,0.5,0))
box()
grid(52,24)

dev.copy(png,'sunset-graph.png')
dev.off()


.jinit()
.jaddClassPath(".")
.jaddClassPath("velocity-1.7.jar")
.jaddClassPath("velocity-tools-2.0.jar")
.jaddClassPath("commons-logging-1.1.3.jar")
.jaddClassPath("log4j-1.2.17.jar")
.jaddClassPath("commons-collections-3.2.1.jar")
.jaddClassPath("commons-io-2.4.jar")
.jaddClassPath("commons-lang-2.4.jar")
obj = .jnew(J("lv.ddgatve.templates.velocity.VelocityMergeRjava"))

result <- .jcall(obj,"Ljava/lang/Integer;","setTemplateName",as.character("sunset-template.vm"))
result <- .jcall(obj,"Ljava/lang/Integer;","setOutFile",as.character("sunset-report.html"))
result <- .jcall(obj,"Ljava/lang/Integer;","setStringParam",as.character("location"),as.character("R\u012Bga, 56\u00B057'N 24\u00B06'E"))
result <- .jcall(obj,"Ljava/lang/Integer;","setStringParam",as.character("figureLocation"),as.character("sunset-graph.png"))

m <- c("Janv\u0101ris", "Febru\u0101ris", "Marts", "Apr\u012Alis", "Maijs", "J\u016bnijs", "J\u016blijs", "Augusts", "Septembris", "Oktobris", "Novembris", "Decembris")
perc <- c("11%", "12%", "13%", "14%", "15%", "16%", "17%", "18%", "19%", "20%", "21%", "22%")
result <- .jcall(obj, "Ljava/lang/Integer;","setArrayParam",as.character("months"), .jarray(m), evalArray=FALSE)
result <- .jcall(obj, "Ljava/lang/Integer;","setArrayParam",as.character("percentages"), .jarray(perc), evalArray=FALSE)
result <- .jcall(obj,"Ljava/lang/Integer;","velocityMerge")

